/**
 * 
 */
package org.agpsoft.simpledatareceiver;

import java.io.IOException;
import java.io.OutputStream;

import org.agpsoft.datareceiverinfrastructure.common.RawData;
import org.agpsoft.datareceiverinfrastructure.exception.DataReceiverCloseException;
import org.agpsoft.datareceiverinfrastructure.exception.DataReceiverOpenException;
import org.agpsoft.datareceiverinfrastructure.exception.DataReceiverRecordException;
import org.agpsoft.datareceiverinfrastructure.recorder.IRawDataRecorder;

/**
 * @author AGP
 *
 */
public class SimpleRawDataRecorder implements IRawDataRecorder {

	private OutputStream outputStream;
	
	/* (non-Javadoc)
	 * @see org.agpsoft.datareceiverinfrastructure.recorder.IRawDataRecorder#recordData(org.agpsoft.datareceiverinfrastructure.common.RawData)
	 */
	@Override
	public void recordData(RawData rawData) throws DataReceiverRecordException {
		try {
			outputStream.write( rawData.getData() );
		} catch (IOException e) {
			throw new DataReceiverRecordException();
		}
	}

	/* (non-Javadoc)
	 * @see org.agpsoft.datareceiverinfrastructure.common.IElementInitiator#close()
	 */
	@Override
	public void close() throws DataReceiverCloseException {
	}

	/* (non-Javadoc)
	 * @see org.agpsoft.datareceiverinfrastructure.common.IElementInitiator#open(java.lang.Object)
	 */
	@Override
	public void open(Object context) throws DataReceiverOpenException {
		SimpleDataReceiverContext basicDataReceiverContext = 
			(SimpleDataReceiverContext) context;
		outputStream = basicDataReceiverContext.getRawRecordStream();
	}

}
